﻿--[[
   @brief    GroupBannerList
   @author   vincentlao
   @version  1.0
   @date     2015-06-20
]]

ierd_tgp.tpf_ui.Require("social\\friend_list\\friend_list.lua");
ierd_tgp.tpf_ui.Require("social\\friend_list\\friend_guide.lua");
ierd_tgp.tpf_ui.Require("social\\talk_dialog\\control.lua");

friend_list_view = 
{
    root_id_ = 0;
    inited_ = false;
    is_mini_ui_ = nil;
    cur_allow_query_friend_status = FALSE;

    show = function()
        if friend_list_view.inited_ then
            return;
        end

        base_utility.log("friend_list init, root_id:"..tostring(friend_list_view.root_id_));
        friend_list_view.inited_ = true;

        friend_list_view.update_tips_area();

        local path_str,path_hoder = U8toCUIStr("FriendList");
        local friend_list_id = ui_utility.uilib():GetControlIDbyPath(path_str, friend_list_view.root_id_);
        FriendBannerList:attach(ui_utility.uilib(), friend_list_id);

        local path_guide_str,path_hoder = U8toCUIStr("UserObject_FriendGuide");
        local obj_id = ui_utility.uilib():GetControlIDbyPath(path_guide_str, friend_list_view.root_id_);
        friend_guide:init(obj_id);

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval('friend_list_view.on_timer', 1000 * 5);
        friend_list_view.on_timer();
    end;

    on_im_inited = function()    
        base_utility.log("[friend_list_view]on_im_inited");         
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:get_friend_list();
    end;

    on_control_create = function(uilib, ctrl_id)
        base_utility.log("[friend_list_view]friend_list on_control_create");
        friend_list_view.root_id_ = ctrl_id;
        friend_list_view.is_ctrl_create = true;
    end;

    --mini 模式加载完成
    on_mini_mode_load_finish = function()
        friend_list_view.init_ui();
        friend_list_view.update_ui();
        comm_center.send_broadcast('i_broadcast_refresh_social_friend_tab_reddot', {});
    end;

    --灰度图片显示
    show_test_image = function()
        -- 灰度策略        
        local path_str,path_hoder = U8toCUIStr("TinyContainer_Test");
        local id = ui_utility.uilib():GetControlIDbyPath(path_str, friend_list_view.root_id_);
        if id > 0 then
            base_utility.log("[friend_list_view][show_test_image] user level:"..tostring(friend_list_model.social_user_level), true);
            ui_utility.setproperty_by_id(id, "Visible", "true");
            friend_list_view.show_loading("false");
        end
    end;

    init_ui = function()
        friend_list_view.show();
    end;

    update_ui = function()
        --获取灰度信息完成否， 
        if friend_list_model.is_get_social_test_info == true then
            base_utility.log("[friend_list_view]update_ui user level:"..tostring(friend_list_model.social_user_level), true);
            --如果是非灰度用户，显示“内测中”
            if friend_list_model.social_user_level == 0 then
                friend_list_view.show_test_image();
                friend_list_view.show_loading("false");
                return;
            end
        else
            base_utility.log("[friend_list_view] update_ui friend_list_model.is_get_social_test_info = false")
            return;
        end

        friend_list_view.update_tips_area();
        
        if friend_list_model.mini_mode_loaded == true then
            -- 界面准备OK
            friend_list_view.update_title();
            --搜索按钮小红点
            friend_list_view.show_friend_search_btn_reddot_imp();

            if friend_list_model.is_get_friend_list_done == true then

                local friend_list = friend_list_model.get_friend_list();

                if type(friend_list) == "table" and #friend_list > 0 then
                    FriendBannerList:show_friend_list(); 
                    friend_guide:show(FALSE);
                    FriendBannerList:show(TRUE);
                else
                    friend_guide:show(TRUE);
                    FriendBannerList:show(FALSE);
                end
                friend_list_view.show_loading("false");
            end
        end
    end;

    update_title = function()
        if friend_list_model.mini_mode_loaded == true then
            local s_title = LoadStr("SOCIAL_FRIEDN_LIST_SEARCH_TITLE");
            local str_count = string.format("%s(%d)", s_title, friend_list_model.get_friend_count());
            MainCommon.set_static_text(ui_utility.uilib(), friend_list_view.root_id_, "Container_FriendInfo\\Static_FriendInfo", str_count);
        end
    end;

    --邀请码区域
    update_tips_area = function()
        --一级种子用户
        if login_model.social_user_level == 1 or login_model.social_user_level == 2 then
            if login_model.social_user_level == 1 and login_model.social_invite_code_count > 0 then
                local path_str,path_hoder = U8toCUIStr("TinyButton_InviteArea");
                local id = ui_utility.uilib():GetControlIDbyPath(path_str, friend_list_view.root_id_);
                if id > 0 then
                    ui_utility.setproperty_by_id(id, "Visible", "true");
                end

                --邀请码个数
                path_str,path_hoder = U8toCUIStr("TinyButton_InviteArea\\TinyButton_InviteCode\\TinyStatic_1"); 
                id = ui_utility.uilib():GetControlIDbyPath(path_str, friend_list_view.root_id_);
                if id > 0 then
                    ui_utility.setproperty_by_id(id, "Caption", tostring(login_model.social_invite_code_count));
                end
            else
                local path_str,path_hoder = U8toCUIStr("TinyButton_Report");
                local id = ui_utility.uilib():GetControlIDbyPath(path_str, friend_list_view.root_id_);
                if id > 0 then
                    ui_utility.setproperty_by_id(id, "Visible", "true");
                end
            end
        end
    end;

    show_loading = function(should_show)
        base_utility.log("[friend_list_view]show_loading:"..tostring(should_show), true);
        local path_str,path_hoder = U8toCUIStr("TinyContainer_Loading");
        local id =  ui_utility.uilib():GetControlIDbyPath(path_str, friend_list_view.root_id_);
        if id ~= nil and id ~= 0 then
           ui_utility.setproperty_by_id(id, "Visible", should_show);
        end   
    end;

    get_friendlist_done = function(event_id, json_str)
        friend_list_model.is_get_friend_list_done = true;

        local friend_table = base_utility.json_to_table(json_str)
        if friend_table.friends and #friend_table.friends > 0 then
            base_utility.log(string.format("[get_friendlist_done] friend count = %d", #friend_table.friends));
            friend_list_model.set_friend_list(friend_table.friends);
        end
        friend_list_view.update_ui();

        friend_list_view.filter_lol_gaming_friends(friend_table.friends);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local friends = friend_list_model.get_friend_list();
        local friend_list = {};
        for i_friend = 1, #friends do
            table.insert(friend_list, friends[i_friend].friend_id);
        end

        local table_param = {};
        table_param["id_list"] = friend_list;

        social_network:get_friend_unread_msg_num(base_utility.table_to_json(table_param));
    end;

    get_friendlist_detail_done = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str);
        if table_param == nil then
            return;
        end

        friend_list_info = table_param.friends;
        if friend_list_info == nil then
            return;
        end

        FriendBannerList:on_get_friendlist_detail_done(friend_list_info);
    end;    

     get_friendlist_stutas_done = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str);
        if table_param == nil then
            return;
        end

        friend_list_status = table_param.friends;
        if friend_list_status == nil then
            return;
        end

        FriendBannerList:on_get_friendlist_stutas_done(friend_list_status);
    end;    

    on_get_friendlist_unread_msg_count = function(event_id, json_str)
        friend_list_model.has_get_unread_msg_count = true;
        local table_param = base_utility.json_to_table(json_str);
        if table_param == nil then
            return;
        end

        if table_param.result ~= 0 then
            return;
        end

        friend_list_unread_msg = table_param.friends;
        if friend_list_unread_msg == nil then
            return;
        end

        FriendBannerList:on_get_friendlist_unread_msg_count(friend_list_unread_msg);
    end;  

    on_recv_friend_report = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str);
        --被删除好友通知
        if (table_param.nType == 0x2)  then
            friend_list_view.remove_friend(table_param.sender); 
          
            friend_list_view.update_ui();

            talk_dialog_control.on_delete_friend_other(table_param.sender);

            comm_center.send_broadcast('i_broadcast_im_on_del_friend', {ret=0, friend_id=table_param.sender}); -- to cross

        elseif table_param.nType == 0xFF  then  
            friend_list_view.update_friend_online_status(table_param["szRemark"]);
        end

    end;

    update_friend_online_status = function(json_str)
        --{"type":"status_change","uin":"391022485","tgp_id":"485","show_id":"300485","gameid":"55555","status":"1","sub_status":"0"}
        local extend_msg = base_utility.json_to_table(json_str);
        if extend_msg.status ~= nil and extend_msg.tgp_id ~= nil then
            local real_status = -1;
            if extend_msg.status == "1" then --上线
                real_status = 0;
            elseif extend_msg.status == "-1" then  --离线
                real_status = 1
            else
                base_utility.log("[friend_list_view][on_recv_friend_report] error:"..tostring(extend_msg.status), true);
            end

            if real_status ~= -1 then
                FriendBannerList:update_friend_online_status(extend_msg.tgp_id, extend_msg.gameid, real_status, tonumber(extend_msg.sub_status));
            end

            comm_center.send_broadcast('i_broadcast_im_on_user_status_changed', {tgp_id=extend_msg.tgp_id, game_id=extend_msg.gameid, status=extend_msg.status, sub_status=extend_msg.sub_status}); -- to cross
        else
            base_utility.log("[friend_list_view][on_recv_friend_report] error! type:0xFF", true);
        end
    end;

    update_friend_online_status_new_im = function(extend_msg)
        if extend_msg.status ~= nil and extend_msg.tgp_id ~= nil then
            local real_status = -1;
            if extend_msg.status == "1" then --上线
                real_status = 0;
            elseif extend_msg.status == "-1" then  --离线
                real_status = 1
            else
                base_utility.log("[friend_list_view][on_recv_friend_report] error:"..tostring(extend_msg.status), true);
            end

            if real_status ~= -1 then
                FriendBannerList:update_friend_online_status(extend_msg.tgp_id, extend_msg.gameid, real_status, tonumber(extend_msg.sub_status));
            end

            comm_center.send_broadcast('i_broadcast_im_on_user_status_changed', {tgp_id=extend_msg.tgp_id, game_id=extend_msg.gameid, status=extend_msg.status, sub_status=extend_msg.sub_status}); -- to cross
        else
            base_utility.log("[friend_list_view][on_recv_friend_report] error! type:0xFF", true);
        end
    end;

    -- 别人同意你的好友申请
    on_add_friend_accepted = function(event_id, json_str)
        local friend_info = base_utility.json_to_table(json_str);

        if friend_info == nil then
            return;
        end
        
        friend_list_view.insert_friend(friend_info);

        friend_list_view.re_sort_friend();

        friend_list_view.update_ui();

        local param = {};
        param["friend_id"] = friend_info.friend_id;
        event_center.send_event(group_event.ADD_FRIEND_INTO_FRIEND_LIST,base_utility.table_to_json(param));
        comm_center.send_broadcast('i_broadcast_im_on_report_add_friend', {ret=0, action=1, friend_id=friend_info.friend_id}); -- to cross
    end;

    -- 同意别人的好友申请
    on_add_friend_accepting = function(event_id, json_str)
        local friend_info = base_utility.json_to_table(json_str);

        if friend_info == nil then
            return;
        end
        
        friend_list_view.insert_friend(friend_info);

        friend_list_view.re_sort_friend();

        friend_list_view.update_ui();

        local param = {};
        param["friend_id"] = friend_info.friend_id;
        event_center.send_event(group_event.ADD_FRIEND_INTO_FRIEND_LIST,base_utility.table_to_json(param));
        comm_center.send_broadcast('i_broadcast_im_on_report_add_friend', {ret=0, action=1, friend_id=friend_info.friend_id}); -- to cross
    end;

    on_add_new_friend = function(friend_info)
        if friend_info == nil then
            return;
        end
        
        friend_list_view.insert_friend(friend_info);

        friend_list_view.re_sort_friend();

        friend_list_view.update_ui();

        local param = {};
        param["friend_id"] = friend_info.friend_id;
        event_center.send_event(group_event.ADD_FRIEND_INTO_FRIEND_LIST,base_utility.table_to_json(param));
        comm_center.send_broadcast('i_broadcast_im_on_report_add_friend', {ret=0, action=1, friend_id=friend_info.friend_id}); -- to cross
    end;

    on_del_friend = function(friend_id) -- 新IM
        friend_list_view.remove_friend(friend_id);
        friend_list_view.update_ui();
        comm_center.send_broadcast('i_broadcast_im_on_del_friend', {ret=0, friend_id=friend_id}); -- to cross
    end;

    on_search_clicked = function(uilib, ctrl_id)       
        base_utility.log("on_search_clicked");

        if login_model.im_login_infomation == nil then
            base_utility.log("on_search_clicked, info is nil");
            return;
        end

        comm_center.call_service('i_service_tqos_friend_search_menu_click', {});

        local btn = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
        local rc = btn:GetRectEx(TRUE,TRUE);
        local param = {x = rc.right, y = rc.bottom};
        event_center.send_event(group_event.SHOW_FRIEND_SEARCH_MENU,base_utility.table_to_json(param));     
    end;

    show_friend_search_btn_reddot = function(bShow)
        friend_list_model.friend_search_menu_reddot = bShow;
        friend_list_view.show_friend_search_btn_reddot_imp();
    end;

    show_friend_search_btn_reddot_imp = function()
        -- local ui_str, str_holder = U8toCUIStr("Container_FriendInfo\\TinyButton_RedBtn");
        -- local id = ui_utility.uilib():GetControlIDbyPath(ui_str, friend_list_view.root_id_);
        -- if id > 0 then
        --     ui_utility.setproperty_by_id(id, "Visible", friend_list_model.friend_search_menu_reddot);
        -- end
        comm_center.call_service('i_service_social_pannel_bottom_menu_show_reddot', {is_show = friend_list_model.friend_search_menu_reddot});
    end;


    on_delete_friend = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str);

        if table_param.result ~= 0 then
            return;
        end

        friend_list_view.remove_friend(table_param.friend_id);
        friend_list_view.update_ui();
        comm_center.send_broadcast('i_broadcast_im_on_del_friend', {ret=0, friend_id=friend_id}); -- to cross
    end;  

    fire_friend_count_changed = function()
        local table_param = {};
        event_center.send_event(group_event.FRIEND_COUNT_CHANGED,base_utility.table_to_json(table_param));
    end;

    -- 定时检查好友列表是否可见
    on_timer = function()
        local is_allow_query = friend_list_view.is_allow_query_lol_status();
        if TRUE == is_allow_query then
             -- 开始轮询lol好友列表的游戏状态
            local lol_friend_list_tbl = comm_center.call_service(
                "i_service_lol_game_status_get_lol_gaming_friend_list", {});
            friend_list_view.handle_set_query_lol_friend_list(lol_friend_list_tbl["lol_friend_list"]);
        end
        if friend_list_view.cur_allow_query_friend_status ~= is_allow_query then
            friend_list_view.cur_allow_query_friend_status = is_allow_query;
            if friend_list_view.cur_allow_query_friend_status ~= TRUE then                
                -- 结束轮询lol好友列表的游戏状态
                friend_list_view.handle_set_query_lol_friend_list({});
            end
        end
    end;
    -- 从好友列表中找出正在玩lol的好友
    filter_lol_gaming_friends = function(friend_list_tbl_param)
        local lol_friend_list = {};
        if friend_list_tbl_param and #friend_list_tbl_param > 0 then
            for i, friend in ipairs(friend_list_tbl_param) do
                local playing_game_str = friend.playing_game;
                -- 好友列表必将多时，这里
                if string.find(playing_game_str, "\"id\":26") ~= nil then
                    table.insert(lol_friend_list, friend.friend_id);
                end
            end
        end
        -- 如果好友面板打开了，则设置查询
        if TRUE == friend_list_view.is_allow_query_lol_status() then
            friend_list_view.handle_set_query_lol_friend_list(lol_friend_list);
        end
    end;
    -- 启动或者停止查询好友的lol状态
    handle_set_query_lol_friend_list = function(lol_friend_list_tbl)
        if nil == lol_friend_list_tbl then
            lol_friend_list_tbl = {};
        end
        -- 只查询在好友列表可见范围之内的好友
        local ret_visible_friends = friend_vlist_control.get_visible_friend_list();
        local query_lol_friens = {};
        if lol_friend_list_tbl and #lol_friend_list_tbl > 0 then
            for i, friend in ipairs(lol_friend_list_tbl) do
                for j, tgp_id in ipairs(ret_visible_friends) do
                    if friend == tgp_id then
                        table.insert(query_lol_friens, tgp_id);
                        break;
                    end
                end
            end
        end

        local data = {};
        data['friend_list'] = query_lol_friens;
        -- 屏蔽lol的状态轮询
        -- comm_center.call_service("i_service_social_info_center_set_lol_gaming_friend_list", data);
    end;

    -- 判断是否需要查询好友状态：好友列表可见 && Dialog没有最小化
    is_allow_query_lol_status = function()
        local is_allow_query = FALSE;
        local pMainFrame_Dialog_ = ui_utility.get_ctrl_ptr_by_cid(CID_SOCIAL_PANEL_DLG);
        if pMainFrame_Dialog_ ~= nil then
            local rc_mainframe = pMainFrame_Dialog_:GetRectEx(FALSE,TRUE);
            local height = rc_mainframe.bottom - rc_mainframe.top;
            if height ~= 0 and friend_list_view.root_id_ ~= Tenio.INVALID_CONTROLID then
                local p_friend_list = ui_utility.get_ctrl_ptr_by_id(friend_list_view.root_id_);
                if p_friend_list ~= nil then
                    is_allow_query = p_friend_list:IsVisible();
                end
            end
        end
        return is_allow_query;
    end;
    --迁移到大界面适配函数-----------------------------------------------------------
    insert_friend = function(friend_info)
        local friend_old = friend_list_model.get_friend_info(friend_info.friend_id);
        if friend_old ~= nil then 
            friend_list_model.remove_friend_info_by_id(friend_info.friend_id);
            friend_list_model.add_friend_info(1, friend_info);
        else
            friend_list_model.add_friend_info(1, friend_info);
            if friend_list_model.mini_mode_loaded == true then
                FriendBannerList:insert_friend(0, friend_info);
            end
        end
    end;

    remove_friend = function(friend_id)
        friend_list_model.remove_friend_info_by_id(friend_id);
        -- if friend_list_model.mini_mode_loaded == false then
        --     friend_list_model.remove_friend_info_by_id(friend_id);
        -- else
        --     FriendBannerList:remove_friend(friend_id);  
        -- end

        local count_friend = friend_list_model.get_total_unread_msg_count();
        local count_group = group_list_model.get_total_unread_msg_count();
        local total = count_friend + count_group;
        if total == 0 then
            local show_red_point = {};
            show_red_point.show = "false";  
            event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json(show_red_point)); 
        end

        local table_param = {};
        table_param["friend_id"] = friend_id;
        event_center.send_event(group_event.FRIEND_CLEAR_MESSAGE,base_utility.table_to_json(table_param));
    end;

    re_sort_friend = function()
        if friend_list_model.mini_mode_loaded == true then
            FriendBannerList:re_sort_friend();
        end
    end;
    ------------------------------------------------------------
}

